<?php
/**
 * Created by PhpStorm.
 * User: lzq
 * Date: 2015/12/24
 * Time: 20:22
 */

namespace App\Http\Controllers\User;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Session;
use BaseClass\Component\Message\Message;


class MessageController extends Controller {
    /**
     * 查询信息 如没有查询限制则置$query_limit=null
     * @param $query_limit
     * |-send  发送者(user/admin)
     * |-desc(默认asc)
     * |sortByDate 信息发送时间
     * |-size 每页条数
     * |-search 查找关键字
     */
    public function sMessage() {
        session(["nowPage" => "/user_sMessage"]);
        //限制条件
        $query_limit=Request::only('send','desc','sortByDate','search');
        //限制条数
        $query_limit['size']=5;
        $inputData["base_message"]=Message::select($query_limit)["message"];
//        dd($data);
        //dump($inputData);
        return view("User.Message.sMessage", $inputData);
    }

    public function aMessage() {

        $info_array = Request::only('message_title', 'message_data','message_recv_admin','message_recv_user');
        if(Session::has('admin.admin_id'))//发送者是admin
        {
            $info_array['message_send_admin']=session('admin.admin_id');
            $info_array['message_send_user']=null;
        }
        else{                          //发送者是user
            $info_array['message_send_user']=session('user.user_id');
            $info_array['message_send_admin']=null;
        }
        if(Message::add($info_array))
        {
            //发送成功
            return true;
        }
        else{
            //发送失败
            return false;
        }
    }

    public function dMessage($message_id) {

        $message=new Message($message_id);
        if($message->delete())
        {
            //删除成功
            return true;
        }
        else{
            //删除失败
            return false;
        }
    }

    public function readSingleMessage($message_id) {

        session(["nowPage" => "/user_sMessage"]);
        // dump(session("nowPage"));
        $query_limit['message_id']=$message_id;
        $data=Message::select($query_limit);
//        dd($data);
//        exit();
        return view("User.Message.readSingleMessage", $data);
    }

}